package cn.jucheng.www.yizhudan.view;

import android.content.Intent;
import android.os.Bundle;
import android.os.Message;
import android.provider.Settings;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;

import java.text.ParseException;
import java.util.Date;

import cn.jucheng.www.yizhudan.MyBaseActivity;
import cn.jucheng.www.yizhudan.R;
import cn.jucheng.www.yizhudan.customcontrols.FitHeightButton;
import cn.jucheng.www.yizhudan.customcontrols.FitHeightTextView;
import cn.jucheng.www.yizhudan.utils.DateUtils;

/**
 * Created by w on 2017-12-09.
 */

public class TimeActivity extends MyBaseActivity implements View.OnClickListener{

    FitHeightTextView title;
    FitHeightTextView nowTime;
    FitHeightTextView yourTime;
    FitHeightButton cancel;
    FitHeightButton submit;

    String now_time;
    String time;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_custom);
        initView();
    }

    private void initView() {

        title = findViewById(R.id.title);
        nowTime = findViewById(R.id.now_time);
        yourTime = findViewById(R.id.your_time);
        cancel = findViewById(R.id.cancel);
        submit = findViewById(R.id.submit);

        cancel.setOnClickListener(this);
        submit.setOnClickListener(this);

        now_time = getIntent().getStringExtra("now_time");
        time = getIntent().getStringExtra("time");

        if (!TextUtils.isEmpty(now_time)) {
            nowTime.setText(getString(R.string.now_times) + now_time);
        }
        if (!TextUtils.isEmpty(time)) {
            yourTime.setText(getString(R.string.your_times) + time);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.cancel:
                onExit();
                break;
            case R.id.submit:
                Intent intent = new Intent(Settings.ACTION_DATE_SETTINGS);
                startActivityForResult(intent, 5);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode) {
            case 5:
                String timename = DateUtils.getDefaultTimeZone();
                Date datea;
                try {
                    datea = DateUtils.stringToDate(time, "yyyy-MM-dd HH:mm:ss");//服务器时间
                } catch (ParseException e) {
                    e.printStackTrace();
                    datea = new Date(System.currentTimeMillis());//获取当前时间
                }
                    Intent intent = new Intent(Settings.ACTION_DATE_SETTINGS);
                    startActivityForResult(intent, 5);
                break;
            default:
                break;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // 按两次返回键退出应用程序
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            onExit();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    protected void HandlerMessage(Message msg) {

    }

    @Override
    public void exc() {

    }
}
